import java.awt.Point;
import java.util.Map;

import com.speljohan.rsbot.script.Calculations;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSItem;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class SilkStealer extends Script { // Names the script

	int eatAT = 18;

	public long lastCheck;
	public long checkTime;

	public int[] Food = new int[] { 1895, 1893, 1891, 4293, 2142, 4291, 2140,
			3228, 9980, 7223, 6297, 6293, 6295, 6299, 7521, 9988, 7228, 2878,
			7568, 2343, 1861, 13433, 315, 325, 319, 3144, 347, 355, 333, 339,
			351, 329, 3381, 361, 10136, 5003, 379, 365, 373, 7946, 385, 397,
			391, 3369, 3371, 3373, 2309, 2325, 2333, 2327, 2331, 2323, 2335,
			7178, 7180, 7188, 7190, 7198, 7200, 7208, 7210, 7218, 7220, 2003,
			2011, 2289, 2291, 2293, 2295, 2297, 2299, 2301, 2303, 1891, 1893,
			1895, 1897, 1899, 1901, 7072, 7062, 7078, 7064, 7084, 7082, 7066,
			7068, 1942, 6701, 6703, 7054, 6705, 7056, 7060, 2130, 1985, 1993,
			1989, 1978, 5763, 5765, 1913, 5747, 1905, 5739, 1909, 5743, 1907,
			1911, 5745, 2955, 5749, 5751, 5753, 5755, 5757, 5759, 5761, 2084,
			2034, 2048, 2036, 2217, 2213, 2205, 2209, 2054, 2040, 2080, 2277,
			2225, 2255, 2221, 2253, 2219, 2281, 2227, 2223, 2191, 2233, 2092,
			2032, 2074, 2030, 2281, 2235, 2064, 2028, 2187, 2185, 2229 };

	public RSTile TradingGuy = new RSTile(2655, 3302);

	RSTile[] BankPath = { new RSTile(2657, 3301), new RSTile(2650, 3292),
			new RSTile(2654, 3283) };

	RSTile[] BackPath = reversePath(BankPath);

	int antiBan() {
		final int GambleInt = random(1, 6);

		switch (GambleInt) {
		case 1:
			wait(random(1000, 1500));
			break;

		case 2:
			if (random(1, 4) == 1) {
				final int x = random(0, 750);
				final int y = random(0, 500);
				moveMouse(0, 0, x, y);
			}
			return random(1000, 1500);

		case 3:
			// Is the current tab the inventory?
			if (getCurrentTab() != Constants.TAB_INVENTORY) {
				// No, so switch to the inventory tab.
				openTab(Constants.TAB_INVENTORY);
				return random(500, 750);
			} else {
				// No, so return
				return random(500, 750);
			}

		case 4:
			// If the player is moving, then abort.
			if (getMyPlayer().isMoving()) {
				return random(750, 1000);
			}

			if (System.currentTimeMillis() - lastCheck >= checkTime) {
				lastCheck = System.currentTimeMillis();
				checkTime = random(60000, 180000);

				if (getCurrentTab() != Constants.TAB_STATS) {
					openTab(Constants.TAB_STATS);
				}
				moveMouse(625, 336, 6, 9);
				return random(5000, 8000);
			}

		case 5:
			if (random(1, 8) == 2) {
				int angle = getCameraAngle() + random(-90, 90);
				if (angle < 0) {
					angle = 0;
				}
				if (angle > 359) {
					angle = 0;
				}

				setCameraRotation(angle);
			}
			return random(500, 750);
		}

		return random(500, 1000);
	}

	private void eat(final int id) {
		log("Eating: " + id + ".");
		atInventoryItem(id, "Eat");
		wait(random(1500, 2500));
		return;
	}

	private void EatCheck() {
		final RSItem food = getInventoryItemByID(Food);
		if (getInventoryCount(food.getID()) >= 1) {
			eat(food.getID());
		} else {
			log("Please don't post bugs on forums");
		}
		return;
	}

	@Override
	public String getAuthor() { // Sets the Authors name
		return "Hellfish and Nvv";
	}

	@Override
	public String getName() { // Sets the Scripts name
		return "SilkStealer";
	}

	@Override
	public String getScriptCategory() { // Sets the category that the script
		// fit's into
		return "Thieving"; // ex Woodcutting, Fishing, Money Making, Quests
	}

	@Override
	public String getScriptDescription() { // Shows a description of the script
		// inside the script dump area
		String html = "<html>";
		html += "<body>";
		html += "Description";
		html += "</body>";
		html += "</html>";
		return html;
	}

	@Override
	public double getVersion() { // Sets the Version number of the script
		return 1.0;
	}

	// Modified Anti-Ban from ProFisher2

	@Override
	public int loop() { // This is the area of the script where you code out the
		// things you want the bot to do

		if (getMyPlayer().isMoving()) {
			antiBan();
			return random(100, 150);
		}

		if (getMyPlayer().isInCombat()) {
			NeedEat();
			return random(1000, 1500);
		}
		if (distanceTo(TradingGuy) < 3) {
			final RSTile tile = new RSTile(2658, 3302);
			atTile(tile, "Walk Here");

			log("Please don't post bugs on forums");

		}
		if (isInventoryFull()) {
			walkPathMM(randomizePath(BankPath, 2, 2), 13);

			final RSObject Bank = findObject(34752);

			if (Bank != null && distanceTo(Bank.getLocation()) <= 10) {
				atObject(Bank, "Use-quickly Bank booth");

				log("Please don't post bugs on forums");

				if (RSInterface.getInterface(Constants.INTERFACE_BANK)
						.isValid()) {

					final int DepositX = random(400, 410);
					final int DepositY = random(305, 310);
					clickMouse(DepositX, DepositY, true);

					final int WithdrawX = random(54, 60);
					final int WithdrawY = random(110, 120);
					clickMouse(WithdrawX, WithdrawY, true);

					final int WithdrawOX = random(54, 60);
					final int WithdrawOY = random(110, 120);
					clickMouse(WithdrawOX, WithdrawOY, true);

					final int WithdrawOOX = random(54, 60);
					final int WithdrawOOY = random(110, 120);
					clickMouse(WithdrawOOX, WithdrawOOY, true);

					final int WithdrawOOOX = random(54, 60);
					final int WithdrawOOOY = random(110, 120);
					clickMouse(WithdrawOOOX, WithdrawOOOY, true);

					final int BankCloseX = random(488, 490);
					final int BankCloseY = random(32, 40);
					clickMouse(BankCloseX, BankCloseY, true);

				}
			}
		}

		if (!isInventoryFull()
				&& !RSInterface.getInterface(Constants.INTERFACE_BANK)
						.isValid()) {
			walkPathMM(randomizePath(BackPath, 2, 2), 13);
			final RSObject stall = findObject(34383);
			if (stall != null && !getMyPlayer().isInCombat()
					&& distanceTo(stall.getLocation()) <= 7) {
				final Point First = new Point(Calculations.tileToScreen(stall
						.getLocation()));
				final Point Second = new Point(First.x + random(-3, 3), First.y
						+ random(-5, 5));
				moveMouse(Second);
				clickMouse(true);
				wait(2000);
				turnToObject(stall);

			} else {

			}
		}

		return random(100, 150);
	}

	void NeedEat() {
		final int php = skills.getCurrentSkillLevel(3);
		if (php <= eatAT) {
			EatCheck();
		} else {
			wait(random(20, 30));
		}
	}

	@Override
	public void onFinish() { // Loads any commands to do when the script is
		// ended
	}

	@Override
	public boolean onStart(final Map<String, String> args) { // Loads any
		// commands
		// needed when the
		// script is started
		return true;
	}
}